package com.project;

import java.util.*;

/**
 *
 */
public class ABT_System {

    /**
     * Default constructor
     */
    public ABT_System() {
        logFlag = 0;
        uid = 0;
    }

    /**
     *
     */
    protected int logFlag;
    protected int uid;
    protected String name;
    protected String pid;
    protected String email;
    protected String phone;
    protected int identity;
    protected int is_register;
    protected String password;

    /**
     * @param input 输入
     * @return 返回
     */
    public boolean Login(List<String> input) throws ABT_SystemException {
        if (input.size() != 2) {
            return false;
        }
        DB_User user = (DB_User) Creat_DBOP("User");

        input.set(0, "`name`=" + input.getFirst());
        input.set(1, "`password`=" + input.get(1));
        List<String> result = user.find(input);
        if (result.isEmpty()) {
            return false;
        }

        // 获取首个结果字符串
        String firstResult = result.getFirst();
        // 分割字符串获取逗号位置
        String[] parts = firstResult.split(",");
        if (parts.length < 7) {
            return false;
        }
        uid = Integer.parseInt(parts[0]);
        name = parts[1];
        pid = parts[2];
        email = parts[3];
        phone = parts[4];
        identity = Integer.parseInt(parts[5]);
        is_register = Integer.parseInt(parts[6]);
        password = parts[7];

        if (parts[5].equals("2")) {
            logFlag = 2;
        } else {
            logFlag = 1;
        }
        return true;
    }

    /**
     * @param DataBase 输入
     * @return 返回
     */
    protected DB_Operator Creat_DBOP(String DataBase) throws ABT_SystemException {
        return switch (DataBase.trim().toLowerCase()) {
            case "user" -> new DB_UserFactory().create();
            case "order" -> new DB_OrderFactory().create();
            case "flight" -> new DB_FlightFactory().create();
            case "ticket" -> new DB_TicketFactory().create();
            case "airline" -> new DB_AirlineFactory().create();
            default -> throw new ABT_SystemException("数据库名非法");
        };
    }

    /**
     * @return 返回
     */
    public int getLogFlag() {
        return logFlag;
    }

    /**
     * @return 返回
     */
    public int getUid() {
        return uid;
    }

    /**
     * @param input 输入
     */
    public void setUid(int input) {
        uid = input;
        try {
            DB_Operator userDB = Creat_DBOP("User");
            List<String> params = new ArrayList<>();
            params.add("`uid`=" + uid);
            List<String> userInfo = userDB.find(params);
            if (!userInfo.isEmpty()) {
                String[] info = userInfo.getFirst().split(",");
                if (info.length >= 7) {
                    name = info[1];
                    pid = info[2];
                    email = info[3];
                    phone = info[4];
                    identity = Integer.parseInt(info[5]);
                    logFlag = identity == 2? 2 : 1;
                    is_register = Integer.parseInt(info[6]);
                    password = info[7];
                }
            }
        } catch (ABT_SystemException e) {
            System.err.println("出现异常：" + e.getMessage());
        }
    }

    /**
     * @return 返回
     */
    public String getName() {
        return name;
    }

    /**
     * @return 返回
     */
    public String getPid() {
        return pid;
    }

    /**
     * @return 返回
     */
    public String getEmail() {
        return email;
    }

    /**
     * @return 返回
     */
    public String getPhone() {
        return phone;
    }

    /**
     * @return 返回
     */
    public int getIdentity() {
        return identity;
    }

    /**
     * @return 返回
     */
    public int getIs_register() {
        return is_register;
    }

    /**
     * @return 返回
     */
    public String getPassword() {
        return password;
    }
}